package com.neusoft.bizcore.auth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.dto.AuthPermissionDTO;
import com.neusoft.bizcore.auth.service.AuthPermissionService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("权限管理")
@RestController
@RequestMapping(value = "/api/w/auth/permissions")
public class AuthPermissionController {

    @Autowired
    private AuthPermissionService authPermissionService;

    @ApiOperation("查询所有权限")
    @GetMapping
    public ResultListDTO<AuthPermissionDTO> index() {
        final List<AuthPermissionDTO> DTOs = this.authPermissionService.findAll();
        final ResultListDTO<AuthPermissionDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有权限")
    @GetMapping("/p")
    public PageResultDTO<AuthPermissionDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.authPermissionService.search(pageable, searchable));
    }

    @ApiOperation("创建权限")
    @PostMapping
    public ResultDTO<AuthPermissionDTO> create(@RequestBody final AuthPermissionDTO dto) {
        final AuthPermissionDTO result = this.authPermissionService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("修改权限")
    @PutMapping("/{id}")
    public ResultDTO<AuthPermissionDTO> update(@PathVariable final Long id,
            @RequestBody final AuthPermissionDTO dto) {
        final AuthPermissionDTO result = this.authPermissionService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("权限详情")
    @GetMapping("/{id}")
    public ResultDTO<AuthPermissionDTO> details(@PathVariable final Long id) {
        final AuthPermissionDTO dto = this.authPermissionService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除权限")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.authPermissionService.delete(id);
        return ResultDTO.success();
    }
}
